﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using clf.weighing.entity;

namespace weighing
{
    public class CommonDao : BaseDao<BaseEntity>
    {
        #region instance

        private static CommonDao? instance;
        public static CommonDao Instance => instance ??= new CommonDao();

        public override string GetTableName()
        {
            return "";
        }
        #endregion

        public int ClearUserData(bool isContainsUser)
        {
            try
            {
                foreach (TableName table in Enum.GetValues(typeof(TableName)))
                {
                    if (table == TableName.scale_brand || table == TableName.scale_series || table == TableName.sys_permission)
                    {
                        continue;
                    }
                    if (table == TableName.user)
                    {
                        //清除非开发者用户
                        if (isContainsUser)
                        {
                            string sql = $"delete from `{table}` where {UserField.role_type} < {(int)UserRoleType.Developer};";
                            Sqlite.ExecuteNonQuery(sql);
                        }
                    }
                    else if (table == TableName.material_category)
                    {
                        string sql = $"delete from `{table}` where {MaterialCateFiled.id} > 4;";
                        Sqlite.ExecuteNonQuery(sql);
                    }
                    else if (table.ToString().StartsWith("sys_"))//系统默认数据
                    {
                        if (table == TableName.sys_role)
                        {
                            string sql = $"delete from `{table}` where {RoleField.is_resaved} = 0;";
                            Sqlite.ExecuteNonQuery(sql);
                        }
                        else
                        {
                            string sql = $"delete from `{table}` where {MaterialCateFiled.id} > 100;";
                            Sqlite.ExecuteNonQuery(sql);
                        }
                    }
                    else
                    {
                        Console.WriteLine(table.ToString());
                        //清除业务数据
                        string sql = $"truncate table {table}";
                        Sqlite.ExecuteNonQuery(sql);
                    }
                }
                return 1;
            }
            catch (Exception)
            {
                return 0;
            }


        }
    }
}
